// pages/my_comment/index.js
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: getApp().imageBaseURL,
    imageResizeParams: getApp().imageResizeParams,
    active:0,
    list:[],//资讯list
    page:1,
    pagesize:10,
    jidilist:[],//基地list
  },
  goThemeClassDetail(e){
    wx.navigateTo({
      url: "/pages/index_theme_class_detail/index?id=" + e.currentTarget.dataset.id
    })
  },
  onChange(e){
    this.setData({
      active:e.detail.index,
      page:1,
      pagesize:10
    })
    if(e.detail.index == 1){
      this.getJidi()
    }else{
      this.getCollect()
    }
  },
  //基地列表
  async getJidi(){
    let data={}
    data['flag'] = 'theme'
    data['action'] = 'collect_list'
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result && result.forEach((item)=>{
        let arr = item.url.split(",")
        let arrhttp = [];
        arr.forEach((imgItem,ind) => {
          arrhttp.push(api.checkurl(imgItem))
        })
        item.url = arrhttp
      })

      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'jidilist['+(this.data.page-1)+']'
       
        this.setData({
          [str]:result,page
        })
      }

    
    }else {
      this.setData({
        list:[]
      })
      // Toast(message)
    }

  },
  //收藏列表
  async getCollect () {
    let data={}
    data['flag'] = 'info_query_collect'
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result && result.forEach((item)=>{
        item.headimg = api.checkurl(item.headimg)
        item.imgurl = api.checkurl(item.imgurl)
      })
      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'list['+(this.data.page-1)+']'
        console.log(str)
        this.setData({
          [str]:result,page
        })
      }
    }else {
      this.setData({
        list:[]
      })
      // Toast(message)
    }
  },

  clickitem(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/news-detail/index?id='+id,
    })
  },

    async cancel (e) {
      let id = e.currentTarget.dataset.id
      let data={}
      data['flag'] = 'info_like'
      data['id'] = id
      data['type'] = 2
      const {recode, result, message} = await api.send(data)
      if(recode === 'SUCC') {
        Toast.success('取消成功')
        this.setData({page:1})
        this.getCollect(this.data.pagesize)
      }else {
        Toast(message)
      }
    },

 /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCollect(this.data.pagesize)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.active){
      this.getJidi()
    }else{
      this.getCollect()
    }  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})